package com.shelpe.services.sinbad.service;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shelpe.services.sinbad.entity.jpa.vo.ConvertKeywordVo;
import com.shelpe.services.sinbad.entity.jpa.vo.KeywordsVo;
import com.shelpe.services.sinbad.entity.jpa.vo.PlatformVo;

@Service
public class PlatformService {

	@Autowired
	private ObjectMapper objectMapper;

	public PlatformVo unserialize(String platform) { // 反序列化，对象到字符串的过程
		try {
			return objectMapper.readerFor(PlatformVo.class).readValue(platform);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String serialize(PlatformVo platform) { // 序列化，字符串到对象的过程 跟数据库交互
		try {
			return objectMapper.writeValueAsString(platform);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return "";
	}

	public KeywordsVo.Vo unserializeKeyword(String oldNote) { // 反序列化，对象到字符串的过程
		try {
			if (oldNote != null) {
				return objectMapper.readerFor(KeywordsVo.Vo.class).readValue(oldNote);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String serializeKeyword(ConvertKeywordVo note) { // 序列化，字符串到对象的过程
															// 跟数据库交互
		try {
			return objectMapper.writeValueAsString(note);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return "";
	}
}
